package cn.cxy.notepad.ui

import cn.cxy.notepad.R
import cn.cxy.notepad.db.bean.Note
import kotlinx.android.synthetic.main.activity_edit_note.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 修改笔记
 */
class EditNoteActivity : BaseNoteActivity() {

    override fun initContent() {
        contentET.setText(note?.content)
    }

    override fun initMenu() {
        listNoteToolbar.menu.findItem(R.id.delNoteMenuItem).isVisible = true
    }

    override fun saveNote(content: String) {
        note?.let {
            if (isModified(it, content)) {
                it.content = content
                GlobalScope.launch {
                    noteRepository.update(it)
                }
            }
        }
    }

    private fun isModified(note: Note, content: String) =
        note.content != content
}
